var app = angular.module("myApp",["globalApp"]);
var type = getQueryString('type');
app.controller("myCtrl",function($scope,$http,getOrder){
	$scope.a = true;
	
	$scope.show1 = $scope.show2 = $scope.show3 = $scope.show4 = $scope.show5 = true;
	
	if(type == "all"){
		$scope.src = {
			"src1" : "image/表单完成-选中.png",
			"src2" : "image/待支付-未选中.png",
			"src3" : "image/待发货-未选中.png",
			"src4" : "image/待收货-未选中.png",
		};
		$scope.active = {
			"a" : true,"b" : false,"c" : false,"d" : false
		};
		$scope.show1 = $scope.show2 = $scope.show3 = $scope.show4 = $scope.show5 = true;
		getOrder.initOrder(1,function(response){
			$scope.arr1 = response.data.orderItems;
		});
		getOrder.initOrder(2,function(response){
			$scope.arr2 = response.data.orderItems;
		});
		getOrder.initOrder(4,function(response){
			$scope.arr3 = response.data.orderItems;
		});
		getOrder.initOrder(5,function(response){
			$scope.arr4 = response.data.orderItems;
		});
		getOrder.initOrder(7,function(response){
			$scope.arr5 = response.data.orderItems;
		});
	}
	if(type == 1){
		$scope.src = {
			"src1" : "image/全部订单-未选中.png",
			"src2" : "image/待支付-选中.png",
			"src3" : "image/待发货-未选中.png",
			"src4" : "image/待收货-未选中.png",
		};
		$scope.active = {
			"a" : false,"b" : true,"c" : false,"d" : false
		};
		$scope.show1 = true;
		$scope.show2 = $scope.show3 = $scope.show4 = $scope.show5 = false;
		getOrder.initOrder(1,function(response){
			$scope.arr1 = response.data.orderItems;
			console.log($scope.arr1)
		});
	}
	if(type == 2){
		$scope.src = {
			"src1" : "image/全部订单-未选中.png",
			"src2" : "image/待支付-未选中.png",
			"src3" : "image/待发货-选中.png",
			"src4" : "image/待收货-未选中.png",
		};
		$scope.active = {
			"a" : false,"b" : false,"c" : true,"d" : false
		};
		$scope.show2 = true;
		$scope.show1 = $scope.show3 = $scope.show4 = $scope.show5 = false;
		getOrder.initOrder(2,function(response){
			$scope.arr2 = response.data.orderItems;
			console.log($scope.arr2)
		});
	}
	if(type == 3){
		$scope.src = {
			"src1" : "image/全部订单-未选中.png",
			"src2" : "image/待支付-未选中.png",
			"src3" : "image/待发货-未选中.png",
			"src4" : "image/待收货-选中.png",
		};
		$scope.active = {
			"a" : false,"b" : false,"c" : false,"d" : true
		}
		$scope.show3 = true;
		$scope.show1 = $scope.show2 = $scope.show4 = $scope.show5 = false;
		getOrder.initOrder(4,function(response){
			$scope.arr3 = response.data.orderItems;
			console.log($scope.arr3)
		});
	}
	
	//返回
	$scope.toReturn = function(){
		window.location.href = window.sessionStorage.http;
	}
	//跳转
	$scope.order = function(x){
		window.location.href = "my_order.html?type=" + x;
	}
	
	//删除订单
	$scope.remove = function(x){
		getOrder.removeOrder(x);
	}
	
	//支付订单
	$scope.toPay = function(x){
		getOrder.toPay(x);
		console.log(1);
	}
	
	//查看详情
	$scope.seeDetail = function(x){
		window.location.href = "orderDetail.html?code=" + x;
	}
	
	//查看物流
	$scope.checkLogistics = function(x){
		window.location.href = "express_information.html?code=" + x;
	}
})


app.factory("getOrder",function($http){
	var obj = {};
	
	//获取订单
	obj.initOrder = function(x,_callback){
		var params = {
			status : x,
			pageSize:10,
			pageNum:1
		};
		params = $.param(params);
		$http({
	        method : "post",
	        dataType: "json",
			withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
		    crossDomain: true,
		    headers:{'Content-Type': 'application/x-www-form-urlencoded'},
		    data: params,
	        async:true,
	        url : host + "/terminal/orderItem/list"
	    }).success(function(response){
	        if(response.code == 2000){
	        	_callback(response);
	        }
	        if(response.code == 4003){
	        	
	        }
	    }).error(function(response){
	    	console.log(response);
	    });
	}
    
    //珊瑚订单
    obj.removeOrder = function(x){
    	var params = {
			code : x
		};
		params = $.param(params);
		$http({
	        method : "post",
	        dataType: "json",
			withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
		    crossDomain: true,
		    headers:{'Content-Type': 'application/x-www-form-urlencoded'},
		    data: params,
	        async:true,
	        url : host + "/terminal/orderItem/cancel"
	    }).success(function(response){
	        if(response.code == 2000){
	        	window.location.reload();
	        }
	    })
    }
    
    obj.toPay = function(x){
    	var params = {
			orderCode : x,
			return_url : "paySuccess.html"
		};
		params = $.param(params);
		$http({
	        method : "post",
	        dataType: "json",
			withCredentials: true,	//跨域名请求。XHR：XMLHttpRequest (XHR) ，基于XML技术的Http请求
		    crossDomain: true,
		    headers:{'Content-Type': 'application/x-www-form-urlencoded'},
		    data: params,
	        async:true,
	        url : host + "/terminal/alipay/mobile"
	    }).success(function(response){
	    	console.log(response);
	        window.sessionStorage.payHtml = response;
		   	location.href = "pay.html";
	    });
    }
    
    return obj;
})

